addpath(genpath('C:\Users\BCoe\matlab_code\new_magnet_tasks'))
clear
clear java
clear global
clear persistent
c=define_colors(uint8(256));

KbQueueCreate();
KbQueueStart;

totalMemory=java.lang.Runtime.getRuntime.totalMemory;
freeMemory=java.lang.Runtime.getRuntime.freeMemory;
if freeMemory/totalMemory<.05
    fprintf('there is only %3.2f%% of free JAVA memory left\n -> exiting... please restart matlab\n',freeMemory/totalMemory*100);
    return
else
    fprintf('started with %3.2f%% of free JAVA memory left\n',freeMemory/totalMemory*100);
end
abs_startUS=round(getsecs*10000);
Memorytime=zeros(1,10000);
[correct, ~, alphac] = imread('Action-ok-icon.png');
[error  , ~, alphae] = imread('Delete-icon.png');
correct(:,:,4) = alphac(:,:);
error(:,:,4) = alphae(:,:);
clear alphac alphae
Parameters={...% variable_name,  value,  'discription',  string, number?
    'sub_inits',	'xxx',		'Subjects 3 Initials (ABC)',                     's';...
    'sub_code',	    '00000',		'Subjects 6 Digit Code (00000)',                 's';...
    'file_nb',		'00',		'File # (or run #, always 2 digits):',           's';...
    'fix_dur',	    '1000',        'Duration of Fixation',                          'n';...
    'tgt_cnt',	    '2',        'Number of Targets',                             'n';...
    'tgt_dur',	    '2',        'Duration of Target',                            'n';...
    'msk_dur',	    '2',        'Duration of Mask',                              'n';...
    'tgt_ecc',	    '2',        'Eccentricity of Target (from FIX in deg''s)',   'n';...
    'res_dur',	    '2',        'Duration for Response ',                        'n';...
    'out_dur',	    '2',        'Duration of Feedback ',                         'n';...
    'trial_limit',  '2',    'Limit of trials (0=permute all)',               'n';...
    'EoB',          '0',            'Eye(0) or Button(1)',                           'n';...
    };
%% screen
try
    oldLevel = Screen('Preference', 'VisualDebugLevel', 1); %level 1: error only
    oldEnableFlag = Screen('Preference', 'SuppressAllWarnings', 1);
    Screen('Preference', 'SkipSyncTests', 1);
    
    %[w, wRect]=Screen('OpenWindow', 2,0);
    %screenpixW=wRect(RectRight);
    %screenpixH=wRect(RectBottom);
    %c_x=round(double(screenpixW/2)); % the stupid text commands require a double for pixels placement
    %c_y=round(double(screenpixH/2));
    %Screen('TextStyle',w,1);% 0=plain, 1=bold, 2=italics, 3=1&2,  4=underlined, 5=4&1,6=4&2, 7=4&3. rem(?,8)
    %Screen('TextFont',w,'Tahoma');
    %Screen('TextSize',w,40);
    %centertextY=c_y-40*.8;
    
    %[w, wRect]=Screen('OpenWindow', screennum, 0,[],32,2);
    %Screen(w,'BlendFunction',GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    %OK=Screen('MakeTexture',w,correct);
    %NG=Screen('MakeTexture',w,error);
    clear correct error
    r_time=0;
    count=fprintf('running\n');
    event_time=round(getsecs*10000)-abs_startUS; % update only once per loop

    
    
    for ii =1:10
        if floor(Memorytime(ii))>=r_time+10 
            r_time=event_time;
            
            %clc; % holy noodles does this eat memory!!!!
            fprintf(repmat('\b',1,count))
            count=fprintf('running\n');
            
        end
        %home;
        [ParamInputs, update] = parameter_inputdlg(Parameters, ['Enter Parameters - ' ] );
        drawnow
        Memorytime(ii)=round(getsecs*10000)-abs_startUS;
        freeMemory(ii)=java.lang.Runtime.getRuntime.freeMemory;

    end
    fprintf('it worked\n\n')
catch badness
    badness.message
    badness.stack
end
fprintf('ended with %3.2f%% of free JAVA memory left\n',freeMemory(end)/totalMemory(end)*100);
Screen('Closeall');
KbQueueStop;
KbQueueRelease;
%int_hist(round(freeMemory/1024)/(totalMemory/1024)*1000);
figure(1);
clf; hold on
plot(freeMemory/totalMemory*100,'b')
%plot([1 diff(Memorytime)]/mean(diff(Memorytime))*10,'r')
if mean(diff(Memorytime))<50 
    plot([1 diff(Memorytime)],'r')
    ylabel('micro seconds')
else
    plot([1 diff(Memorytime)]/10,'r')
    ylabel('milli seconds')
end
plot(freeMemory/totalMemory*100,'b')
ylim([0 100])